#ifndef __DBFILEINDEX_H
#define __DBFILEINDEX_H

#pragma once

#include "DBFileBufPageManager.h"
#include "DBFileType.h"
#include "global.h"

class DBFileIndex
{
	int rootId;

	DBFileBufPageManager* manager;

	void InsertToIndexPage(const char* key, int keyLength
		, DBBufPage* insertBufPage, RowID rowId);

	void DeleteFromIndexPage(int keyIndex, DBBufPage* delBufPage);

	DBBufPage* FindLeafPage(const char* key, int keyLength);

	void DropPage(DBBufPage* bufPage);

public:
	DBFileIndex();
	
	~DBFileIndex(void);

	/**
	* Funtion Description : Initialize the index system
	* @para int inRootId
	* Root index page
	* @para DBFileBufPageManager* inManager
	* Buf Page manager, use it to alloc page
	* by Author: Jimmy
	*/
	void initIndex(int inRootId, DBFileBufPageManager* inManager);

	/**
	* Funtion Description : Drop this table
	* by Author: Jimmy
	*/
	void dropIndex();

	/**
	* Funtion Description :Insert a (key, rowid) pair to b+tree
	* @para char* key
	* key
	* @para int keyLength
	* keylength
	* @para RowID rowId
	* rowid
	* @return
	* true: find and del
	* false: del fail
	* by Author: Jimmy
	*/
	bool insert(const char* key, int keyLength, RowID rowId);

	/**
	* Funtion Description :Delete a (key, rowid) pair to b+tree
	* @para char* key
	* key
	* @para int keyLength
	* keyLength
	* @return
	* true: find and del
	* false: del fail
	* by Author: Jimmy
	*/
	bool del(const char* key, int keyLength);

	/**
	* Funtion Description : Update a pair, (oldkey, oldrowid) -> (newkey, newrowid)
	* @para char* oldKey
	* oldkey
	* @para int oldKeyLength
	* oldkey length
	* @para char* newKey
	* newkey
	* @para int newKeyLength
	* newkey length
	* @para int newRowId
	* new rowid
	* @return
	* true: find and del
	* false: del fail
	* by Author: Jimmy
	*/
	bool update(const char* oldKey, int oldKeyLength, const char* newKey
				, int newKeyLength, RowID newRowId);

	/**
	* Funtion Description : Search a key in b+tree
	* @para char* key
	* key
	* @para int keyLength
	* key length
	* @para RowID& rowid
	* return value, rowid
	* @return bool
	* true: find
	* false: not find
	* by Author: Jimmy
	*/
	bool search(const char* key, int keyLength, RowID& rowid);

	/**
	* Funtion Description : Get the root id
	* @return int
	* rootId
	* by Author: Jimmy
	*/
	int getRootId() const;
};

#endif